#light
#r "PresFirst.dll"
open System
open System.Windows.Forms
open PresFirst

let rec fib = function
    | x when x < 1  -> 0
    | x when x <= 2 -> 1
    | x -> fib (x - 1) + fib (x - 2)
    
let addControl (form: Form) control = form.Controls.Add(control)

type FibModel() =
    interface IFibModel with
        member x.Calculate(num) = fib num

type FibView() =
    inherit Form(Height = 100, Width = 300) as x
    
    let _calcButton = new Button(Text = "Calc", Top = 20, Left = 150)
    let _userText = new TextBox(Top = 20, Left = 45, Width = 30)
    let _resultText = new TextBox(Top = 20, Left = 110, Width = 30, ReadOnly = true)
    let _triggerCalculating, _calculating = IEvent.create()
    
    do addControl x _calcButton
    do addControl x _userText
    do addControl x _resultText
    do addControl x (new Label(Top = 20, Left = 75, Text = ")  =  ", AutoSize = true))
    do addControl x (new Label(Top = 20, Left = 20, Text = "fib (", AutoSize = true))
    do _calcButton.Click
        |> IEvent.listen (fun e -> _triggerCalculating e)

    interface IFibView with
        member x.Title
            with get() = x.Text
            and  set v = x.Text <- v
            
        member x.NumberToCalculate
            with get() = _userText.Text
            
        member x.DisplayResult(result) =
            _resultText.Text <- result
            
        member x.Calculating =
            _calculating

let m = new FibModel()
let v = new FibView()
do new FibPresenter(m, v) |> ignore
do v.Show()